package com.zlf.sb.demo;

import java.util.PriorityQueue;
import java.util.Random;

/***
 *@title Top100LargestNumbers
 *@description <TODO description class purpose>
 *@author wgc_j
 *@version 1.0.0
 *@create 2025-02-13 16:41
 **/
public class Top100LargestNumbers {
    public static int[] top100LargestNumbers(int[] nums) {
        PriorityQueue<Integer> minHeap = new PriorityQueue<>(100);
        for(int num : nums){
            if(minHeap.size() < 100){
                minHeap.offer(num);
            }else if(num > minHeap.peek()){
                minHeap.poll();
                minHeap.offer(num);
            }
        }
        int[] result = new int[100];
        for(int i = 0; i < 100; i++){
            if(minHeap.isEmpty()){
                break;
            }
            result[i] = minHeap.poll();
        }
        return result;
    }

    public static void main(String[] args) {
        int size = 100000000; // 一亿个数字
        int[] numbers = new int[size];
        Random random = new Random();
// 使用循环生成随机数字
        for (int i = 0; i < size; i++) {
            numbers[i] = random.nextInt();
        }

        int[] result = top100LargestNumbers(numbers);
        for(int num : result){
            System.out.print(num + " ");
        }
    }
}
